;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Enterprise.AgentPersonConfig", [])
        .controller("AgentPersonConfigCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            // 列表查询条件
            $scope.condition = {};
            // 表格信息
            $scope.tableConfig = {};

            $scope.markInfo = {
                curRow: -1
            };
            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 人员列表信息
            $scope.dataList = [];

            // 当前选中行
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            // 查询人员列表
            $scope.search = function (curPage) {
                if (curPage) $scope.pageInfo.curPage = curPage;
                getColumnsAndData();
            };

            function getColumnsAndData() {
                var url = "/employeeSetting/configList";
                var param = {
                    filters: {
                        companyNameLikeList: [$scope.condition.companyNameLikeList]
                    },
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                };
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    if (!res.value) return;
                    setHeader(res.value.jobs);
                    setPersonList(res.value.employeeSettingVOs);
                });
            }

            function setHeader(headers) {
                $scope.columnsRes = headers;
                $scope.tableConfig.tableCol = [
                    {
                        name: "小计",
                        dataField: "totalSize",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    }
                ];
                var tableCols = [];
                var tdWidth = 140; // 表格列宽度
                angular.forEach($scope.columnsRes, function (item) {
                    tableCols.push({
                        name: item.name,
                        dataField: item.id,
                        width: tdWidth,
                        isShow: true,
                        isFixed: false,
                        needInput: true,
                    });
                });
                $scope.tableConfig.tableCol = $scope.tableConfig.tableCol.concat(tableCols);
                $scope.tableConfig.fixedWidth = 60 + 140; // 序号和代理商列宽度
                $scope.tableConfig.scrollWidth = $scope.tableConfig.fixedWidth + 100 + tableCols.length * tdWidth;
                $scope.tableConfig.billFixedStyle = { left: $scope.tableConfig.fixedWidth + "px" };
            }

            function setPersonList(value) {
                var persons = [];
                angular.forEach(value.list, function (agentItems) {
                    var tempObj = {};
                    var totalNum = 0;
                    angular.forEach(agentItems, function (personItem) {
                        var tempVal = parseInt(personItem.numberEmp) || 0;
                        totalNum += tempVal;
                        tempObj[personItem.jobId] = personItem;
                        tempObj.orgName = personItem.orgName;
                    });
                    tempObj.totalSize = totalNum;
                    persons.push(tempObj);
                });
                $scope.pageInfo.totalRows = value.total;
                $scope.pageInfo.totalPages = value.pages;
                $scope.dataList = persons;
            }

            $scope.countTotal = function (item) {
                var totalNum = 0;
                angular.forEach($scope.columnsRes, function (v) {
                    var tempVal = parseInt(item[v.id].numberEmp) || 0;
                    totalNum += tempVal;
                });
                $scope.dataList[$scope.markInfo.curRow].totalSize = totalNum;
            };

            $scope.changeNumberEmpk = function (item) {
                if (!$scope.btnPermission.agentPersonConfigSave) return;
                var url = "/employeeSetting/save";
                ybHttp.Post('device', url, item).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.search();
                });
            };


            $scope.importData = function () {
                var param = {
                    name: "人员配置标准",
                    templateReqService: "device",
                    templateReqUrl: "/employeeSetting/export",     // 原始数据模板接口
                    importReqService: "device",
                    importReqUrl: "/employeeSetting/import",      // 导入接口
                    importErrorUrl: "",
                    types: ["xlsx"] 
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getColumnsAndData();
                });
            };


            var btnPermissionCode = {
                agentPersonConfigSave: window.ActionCode.agentPersonConfigSave,
                agentPersonConfigImport: window.ActionCode.agentPersonConfigImport,
            };
            $scope.btnPermission = {
                agentPersonConfigSave: true,
                agentPersonConfigImport: true,
            };
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            // 初始化页面信息
            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                getColumnsAndData();
            })()
        }])
        ;
})(window, window.angular);